package com.aifund.common.service.impl;

import com.aifund.common.dao.SystemConfigDao;
import com.aifund.common.entity.SystemConfig;
import com.aifund.common.enums.config.ConfigEnum;
import com.aifund.common.exception.AiFundException;
import com.aifund.common.service.SystemConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
public class SystemConfigServiceImpl implements SystemConfigService {


    @Autowired
    SystemConfigDao systemConfigDao;

    @Override
    public void save(SystemConfig systemConfig) {
        systemConfigDao.save(systemConfig);
    }

    @Override
    public SystemConfig getByKey(String key) {
       return systemConfigDao.getByConfigKey(key);
    }

    @Override
    public void setTokenDay(Float day) {
        SystemConfig systemConfig = systemConfigDao.getByConfigKey(ConfigEnum.TOKEN_DAY.getConfigKey());
        if(systemConfig==null){
            systemConfig = new SystemConfig();
            systemConfig.setConfigKey(ConfigEnum.TOKEN_DAY.getConfigKey());
            systemConfig.setConfigValue(day+"");
            systemConfigDao.save(systemConfig);
            return;
        }
        systemConfig.setConfigValue(day+"");
        systemConfigDao.save(systemConfig);
    }

    @Override
    public Float getTokenDay() {
        SystemConfig systemConfig = systemConfigDao.getByConfigKey(ConfigEnum.TOKEN_DAY.getConfigKey());
        if(systemConfig == null){
            throw new AiFundException("500","服务器异常","未配置token失效时长");
        }
        return Float.parseFloat(systemConfig.getConfigValue());
    }

    @Override
    public void setJwtSecret(String secret) {
        SystemConfig systemConfig = systemConfigDao.getByConfigKey(ConfigEnum.JWT_SECRET.getConfigKey());
        if(systemConfig==null){
            systemConfig = new SystemConfig();
            systemConfig.setConfigKey(ConfigEnum.TOKEN_DAY.getConfigKey());
            systemConfig.setConfigValue(secret);
            systemConfigDao.save(systemConfig);
            return;
        }
        systemConfig.setConfigValue(secret);
        systemConfigDao.save(systemConfig);
    }

    @Override
    public String getJwtSecret() {
        SystemConfig systemConfig = systemConfigDao.getByConfigKey(ConfigEnum.JWT_SECRET.getConfigKey());
        if(systemConfig == null){
            throw new AiFundException("500","服务器异常","未配置Jwt密钥");
        }
        return systemConfig.getConfigValue();
    }

    @Override
    public BigDecimal getMinCashNum() {

        SystemConfig systemConfig = systemConfigDao.getByConfigKey(ConfigEnum.MIN_CASH_NUM.getConfigKey());
        if(systemConfig == null){
            throw new AiFundException("500","服务器异常","未配置最小提币数量");
        }
        BigDecimal bigDecimal = new BigDecimal(systemConfig.getConfigValue());
        return bigDecimal;
    }

    @Override
    public BigDecimal getMinCashHandlingFee() {
        SystemConfig systemConfig = systemConfigDao.getByConfigKey(ConfigEnum.MIN_CASH_HANDING_FEE.getConfigKey());
        if(systemConfig == null){
            throw new AiFundException("500","服务器异常","未配置最小提现手续费");
        }
        BigDecimal bigDecimal = new BigDecimal(systemConfig.getConfigValue());
        return bigDecimal;
    }

    @Override
    public BigDecimal getMaxCashHandlingFee() {
        SystemConfig systemConfig = systemConfigDao.getByConfigKey(ConfigEnum.MIN_CASH_HANDING_FEE.getConfigKey());
        if(systemConfig == null){
            throw new AiFundException("500","服务器异常","未配置最大提现手续费");
        }
        BigDecimal bigDecimal = new BigDecimal(systemConfig.getConfigValue());
        return bigDecimal;
    }

    @Override
    public BigDecimal getCashPercentage() {
        SystemConfig systemConfig = systemConfigDao.getByConfigKey(ConfigEnum.CASH_PERCENTAGE.getConfigKey());
        if(systemConfig == null){
            throw new AiFundException("500","服务器异常","未配置提现手续费百分比");
        }
        BigDecimal bigDecimal = new BigDecimal(systemConfig.getConfigValue());
        return bigDecimal;
    }

    @Override
    public String getFileServeUrl() {
        SystemConfig systemConfig = systemConfigDao.getByConfigKey(ConfigEnum.FILE_SERVICE_URL.getConfigKey());
        if(systemConfig == null){
            throw new AiFundException("500","服务器异常","未配置提现手续费百分比");
        }
        return systemConfig.getConfigValue();
    }
}
